package com.recipedb.recipedb.repository;

import com.recipedb.recipedb.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
import java.util.Optional;


import java.util.Optional;

public interface UserRepository extends JpaRepository<User, Long> {

    Optional<User> findByUsername(String username);

    boolean existsByUsername(String username);

    // 示例：自定义更新计数方法
    @Modifying
    @Query("update User u set u.followCount = u.followCount + 1 where u.id = :userId")
    void incrementFollowCount(Long userId);

    @Modifying
    @Query("update User u set u.followCount = u.followCount - 1 where u.id = :userId")
    void decrementFollowCount(Long userId);

    @Modifying
    @Query("update User u set u.fanCount = u.fanCount + 1 where u.id = :userId")
    void incrementFanCount(Long userId);

    @Modifying
    @Query("update User u set u.fanCount = u.fanCount - 1 where u.id = :userId")
    void decrementFanCount(Long userId);

    @Modifying
    @Query("update User u set u.likeCount = u.likeCount + 1 where u.id = :userId")
    void incrementLikeCount(Long userId);

    @Modifying
    @Query("update User u set u.likeCount = u.likeCount - 1 where u.id = :userId")
    void decrementLikeCount(Long userId);

    // 根据uid查询用户
    Optional<User> findByUid(String uid);

    // 根据uid或username查询用户列表
    List<User> findByUidOrUsername(String uid, String username);
}
